//
//  ForgetPasswordViewController.m
//  IntelligentHousehold
//
//  Created by wmwMAC on 17/6/23.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "ForgetPasswordViewController.h"

#import "OneTextFieldTableViewCell.h"
#import "CodeTableViewCell.h"

static NSString *identifier = @"scheduleIdentifiercell";

@interface ForgetPasswordViewController ()<UITableViewDelegate,UITableViewDataSource,OneTextFieldTableViewCellDelegate,CodeTableViewCellDelegate>
{
    NSString *phoneNumberStr;
    
    
    NSString *PasswordStr;
    
    BOOL timeOut;//验证码是否过期 1过期 0未过期
    NSString *_telStr;
    NSString *_codeStr;
    NSString *_InputCodeStr;
}

@property (nonatomic, strong) UITableView *tableView;
/**  */
@property (nonatomic , strong) CodeTableViewCell *codeCell;
/**  */
@property (nonatomic , strong) NSString *anewCode;
/**  */
@property (nonatomic , strong) NSString *sureCode;
/**  */
@property (nonatomic , strong) UIButton *btn;
@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"找回密码";
    
    timeOut = NO;//验证码未过期
    
    [self addTableviewUI];
    
    [self createBtn];
    
}

- (void)createBtn{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 80)];
    
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.frame = CGRectMake(30, 15, kUIScreenWidth-60, 50);
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:@"确定" forState:(UIControlStateNormal)];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn setBackgroundImage:[UIImage imageNamed:@"dhl"] forState:(UIControlStateNormal)];
    btn.layer.cornerRadius = 10;
    btn.clipsToBounds = YES;
//    [btn setTitleColor:[UIColor colorWithHexString:@"999999"] forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(sureAction) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:btn];
    
    self.tableView.tableFooterView = view;
}

- (void)sureAction{
    if (timeOut) {
        [MBProgressHUD showText:@"验证码已过期"];
    }else{
        if (![_telStr isEqualToString:self.codeCell.tfString]) {
            [MBProgressHUD showText:@"手机号有误"];
        }else if (![_codeStr isEqualToString:_InputCodeStr]){
            [MBProgressHUD showText:@"验证码有误"];
        }else if (![self.anewCode isEqualToString:self.sureCode]){
            [MBProgressHUD showText:@"两次密码不一致"];
        }else{
        //找回密码接口
            
            [self getPassWd];
            
        }
        
    }
}

- (void)addTableviewUI {
   
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerClass:[OneTextFieldTableViewCell class] forCellReuseIdentifier:identifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"CodeTableViewCell" bundle:nil] forCellReuseIdentifier:@"codeCell"];
    [self.view addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            OneTextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
            cell.delegate = self;
            cell.tag = 2000 + indexPath.section*10+indexPath.row;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.leftTextFiled.placeholder = @"请输入您的手机号";
            
            return cell;
        } else {
            CodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"codeCell" forIndexPath:indexPath];
            self.codeCell = cell;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
            
            return cell;
        }
    } else {
        OneTextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        cell.delegate = self;
        cell.tag = 2000 + indexPath.section*10+indexPath.row;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row == 0) {
            cell.leftTextFiled.placeholder = @"新密码";
        } else {
            cell.leftTextFiled.placeholder = @"请再次确认密码";
        }
        
        cell.leftTextFiled.secureTextEntry = YES;

        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        return 10;
    }else {
        return 0.01;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    
    return footview;
}


- (void)oneTextFieldTableCellPassText:(NSString *)tfString index:(NSInteger)index{
    if (index - 2000 == 0) {
        self.codeCell.tfString = tfString;
    } else if (index - 2000 - 10 == 0){
        self.anewCode = tfString;
    } else {
        self.sureCode = tfString;
    }
}

-(void)CodeTableViewCellPassTimeOut:(NSInteger)outTime CodeStr:(NSString *)codeStr InputCodeStr:(NSString *)inputCodeStr TelStr:(NSString *)telStr{
    if (outTime == 0) {
        timeOut = YES;
    }else if (outTime == 100){
        timeOut = NO;
        _telStr = telStr;
        _codeStr = codeStr;
        _InputCodeStr = inputCodeStr;
    }
}

#pragma mark -------------- 找回密码 --------
-(void)getPassWd{
    NSString *url = [NSString stringWithFormat:FINDPWD,RealmName];
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:_telStr forKey:@"tell"];
    [parameDic setObject:self.anewCode forKey:@"password"];
    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"msg"] isEqualToString:@"success"]) {
            [MBProgressHUD showText:@"修改成功"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUD];
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            [MBProgressHUD showText:[responseObject objectForKey:@"msg"]];
        }
        
        
    } failure:^(NSError *error) {
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
